require 'excel'
class TimeLogsController < ApplicationController
  layout "project"
  before_filter :check_authentication

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)

  def index
       @user = @cur_user
       @user.reload
       @name = @user.username
       @start_time = Time.now.strftime("%I:%M %p")
       @end_time   = "06:00 PM"
       @time_date  = Time.now.strftime("%Y-%m-%d")
       @todos = @project.todos

       if @cur_user.have_power?
       @time_log_pages, @time_logs = paginate :time_logs, :per_page => 10,:conditions=>["project_id = ?",params[:project_id].to_i],:order => "id desc"
       else
       @time_log_pages, @time_logs = paginate :time_logs, :per_page => 10,:conditions=>["user_id = ? AND project_id = ?",@user.id,params[:project_id].to_i],:order => "id desc"
       end
       @time_log = TimeLog.new
       render :partial=>'time_log' if request.post?
  end
  def time_flag
       @user = @cur_user
       @user.reload
       @name = @user.username
       @start_time = Time.now.strftime("%I:%M %p")
       @end_time   = "06:00 PM"
       @time_date  = Time.now
       @todos = @project.todos
       ids = params[:id]
       sub = params[:submit]
       if ids and ids.size > 0
          if sub == "delete"
          ids.each do |i|
	    time =  TimeLog.find(i.to_i)
	    time.destroy
	  end
	  else
          ids.each do |i|
	    time =  TimeLog.find(i.to_i)
	    time.confirmed = (params[:submit] == "confirm" ? 1 : 0)  
	    time.confirmed_by = (params[:submit] == "confirm" ? @cur_user.id : "")  
	    time.confirmed_at = (params[:submit] == "confirm" ? Time.now : "")  
	    time.save
	  end
	  end
       end
       if @cur_user.have_power?
       @time_log_pages, @time_logs = paginate :time_logs, :per_page => 10,:conditions=>["project_id = ?",params[:project_id].to_i],:order => "id desc"
       else
       @time_log_pages, @time_logs = paginate :time_logs, :per_page => 10,:conditions=>["user_id = ? AND project_id = ?",@user.id,params[:project_id].to_i],:order => "id desc"
       end

       render :update do |page|
         page.replace_html 'timelogs',:partial => "time_log", :locals=>{:time_log=>@time_logs}
         if ids and ids.size > 0
            if sub != "delete"
	      ids.each do |i|
	      page.visual_effect :highlight,'entry_' + i.to_s,:duration=>15
	      end
	    end
	  end
       end
  end
  def show_completed_projects 
	   @user = @cur_user
	   @projects = @user.completed_projects
       @time_log_pages, @time_logs = paginate :time_logs, :per_page => 10,:conditions=>["user_id = ? AND project_id in(select id from projects where completed ='Yes')",@user.id],:order => "id desc"
	   render :partial=>'time_log'  if @page == 1 or request.post?
       @time_log = TimeLog.new
	   @time_log.after_work_time=0
  end

  def show_all_projects 
	   @user = @cur_user
	   @projects = @user.projects
       @time_log_pages, @time_logs = paginate :time_logs, :per_page => 10,:conditions=>["user_id = ?",@user.id],:order => "id desc"
	   render :partial=>'time_log'  if @page == 1 or request.post?
       @time_log = TimeLog.new
  end

  def statistic
	   @user = @cur_user
	   @user.reload
	   @name = @user.username
       @time_log_pages, @time_logs = paginate :time_logs, :per_page => 10,:conditions=>["user_id = ?",@user.id],:order => "id desc"
	  
	   @project_timelogs = TimeLog.find(:all,
	                 :conditions => ["user_id =?",@user.id]).group_by{|timelog| timelog.project}
       @time_log = TimeLog.new
  end

  def show
    @time_log = TimeLog.find(params[:id])
  end

  def new
    @user = @cur_user
    @time_log = TimeLog.new
  end

  def create
      @user = @cur_user
      t_date =  params[:time_date]
      start_at = Time.parse(t_date + " - " + params[:start_at].split(" ").first)
      start_at = start_at.since(3600*12) if params[:start_at].split(" ").last.downcase == "pm" 
      end_at   = Time.parse(t_date + " - " + params[:end_at].split(" ").first)
      end_at   = end_at.since(3600*12) if params[:end_at].split(" ").last.downcase == "pm" 
      params[:time_log][:start_at] = start_at 
      params[:time_log][:end_at]   =  end_at

      @time_log = TimeLog.new(params[:time_log])
      @time_log.user = @cur_user
      @save = @time_log.save
      @projects = @user.active_projects
       if @cur_user.have_power?
       @time_log_pages, @time_logs = paginate :time_logs, :per_page => 10,:conditions=>["project_id = ?",params[:project_id].to_i],:order => "id desc"
       else
       @time_log_pages, @time_logs = paginate :time_logs, :per_page => 10,:conditions=>["user_id = ? AND project_id = ?",@user.id,params[:time_log][:project_id].to_i],:order => "id desc"
       end
	render :update do |page|
	  page.hide 'new_time'
	  page.replace_html 'timelogs',:partial => "time_log"
	  page.visual_effect :highlight,'entry_' + @time_log.id.to_s,:duration=>4.5
	end
  end

  def edit
    @user = @cur_user
    @time_log = TimeLog.find(params[:id])
       @todos = @project.todos
	@selected = @time_log.project.id
	if @time_log.activity_type.nil?
	  @selected_type = 0 
	else
	  @selected_type = @time_log.activity_type.id
	end

	logger.error("==========================")
	logger.error(@selected)
	logger.error(@selected_type)
	logger.error("==========================")

    @start_time = @time_log[:start_at].strftime("%I:%M %p")
    @end_time   = @time_log[:end_at].strftime("%I:%M %p") 
    @time_date  = @time_log[:start_at].strftime("%Y-%m-%d")
  end

  def update
    @time_log = TimeLog.find(params[:id])
      t_date =  params[:time_date]
      start_at = Time.parse(t_date + " - " + params[:start_at].split(" ").first)
      start_at = start_at.since(3600*12) if params[:start_at].split(" ").last == "pm" 
      end_at   = Time.parse(t_date + " - " + params[:end_at].split(" ").first)
      end_at   = end_at.since(3600*12) if params[:end_at].split(" ").last == "pm" 
      @time_log.start_at = start_at 
      @time_log.end_at   =  end_at

    if @time_log.update_attributes(params[:time_log])
        redirect_to time_logs_path(params[:time_log][:project_id])
    else
      render :action => 'edit'
    end
  end

  def destroy
    TimeLog.find(params[:id]).destroy
	@user = @cur_user
	  @projects = @user.active_projects
      @time_log_pages, @time_logs = paginate :time_logs, :per_page => 10,:conditions=>["user_id = ? AND project_id in(SELECT id FROM projects WHERE completed = 'No' OR completed is NULL)",@user.id],:order => "id desc"
	render :update do |page|
	  page.replace_html 'timelogs',:partial => "time_log",:locals=>{:page=>2}
	end
  end
  def export 
   begin
     @timelogs = TimeLog.find(:all,:conditions=>["project_id = ?",params[:project_id].to_i],:order => "id desc")
     gen_excel(@timelogs)
   rescue => e
     logger.error "error generate----------------------"
     logger.error e
     logger.error "error generate----------------------"
   end
   if File.exist?(file_name)
   send_file(file_name,
             :type =>'application/vnd.ms-excel',
	     :disposition => 'inline'
	     )
   end

   #render :nothing => true
  end
end
